home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / Miro_Downloader.exe / idlenotifier.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-11-12  |  3.4 KB  |  94 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. """This module provides IdleNotifier objects which run in the background and 
  5.    notify delegate objects about the system idle state. The low-level platform
  6.    specific code which checks the actual idle state must be provided by the
  7.    frontend.
  8.    
  9.    An IdleNotifier object is controlled by two parameters: the idle threshold
  10.    and the periodicity. The periodicity value specifies the frequency at which
  11.    the system's idle state will be checked. When the system has been idling for 
  12.    more than idleThreshold seconds, the delegate's systemHasBeenIdlingSince 
  13.    method is called. When the system is active and is known to have been 
  14.    previously idling, the delegate's systemIsActiveAgain method is called.
  15. """
  16. import time
  17. import eventloop
  18. import idletime
  19. import logging
  20. DEFAULT_PERIODICITY = 5
  21. DEFAULT_IDLE_THRESHOLD = 300
  22.  
  23. class IdleNotifier:
  24.     
  25.     def __init__(self, delegate, idleThreshold = DEFAULT_IDLE_THRESHOLD, periodicity = DEFAULT_PERIODICITY):
  26.         '''Initialize the IdleNotifier object, the passed delegate will be
  27.            called when the system idle state changes.
  28.            - idleThreshold specifies the idle time (in seconds) after which the
  29.            delegate is called.
  30.            - periodicity specifies that that the idle state should be checked
  31.            every X seconds.
  32.         '''
  33.         self.idling = False
  34.         self.wasIdling = False
  35.         self.delegate = delegate
  36.         self.idleThreshold = idleThreshold
  37.         self.periodicity = periodicity
  38.         self.lastTimeout = None
  39.  
  40.     
  41.     def start(self):
  42.         logging.info('idle notifier running')
  43.         self.lastTimeout = eventloop.addTimeout(self.periodicity, self.run, 'Idle notifier')
  44.  
  45.     
  46.     def run(self):
  47.         
  48.         try:
  49.             seconds = int(idletime.get())
  50.         except:
  51.             logging.warning('idletime module returned an invalid value...')
  52.             seconds = 0
  53.  
  54.         if self.idling:
  55.             self._whenIdling(seconds)
  56.         else:
  57.             self._whenNotIdling(seconds)
  58.         self.lastTimeout = eventloop.addTimeout(self.periodicity, self.run, 'Idle notifier')
  59.  
  60.     
  61.     def join(self):
  62.         
  63.         try:
  64.             self.lastTimeout.cancel()
  65.         except:
  66.             pass
  67.  
  68.  
  69.     
  70.     def _whenNotIdling(self, seconds):
  71.         if seconds >= self.idleThreshold:
  72.             logging.info('system has been idling since %d seconds', seconds)
  73.             self.idling = True
  74.             self.wasIdling = True
  75.             if self.delegate is not None and hasattr(self.delegate, 'systemHasBeenIdlingSince'):
  76.                 self.delegate.systemHasBeenIdlingSince(seconds)
  77.             
  78.         
  79.  
  80.     
  81.     def _whenIdling(self, seconds):
  82.         if seconds < self.idleThreshold:
  83.             self.idling = False
  84.             if self.wasIdling:
  85.                 logging.info('system is active again since %d seconds', seconds)
  86.                 if self.delegate is not None and hasattr(self.delegate, 'systemIsActiveAgain'):
  87.                     self.delegate.systemIsActiveAgain()
  88.                     self.wasIdling = False
  89.                 
  90.             
  91.         
  92.  
  93.  
  94.